/**
 * 二叉树的比较
 */

class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const a1 = new TreeNode("A");
const b1 = new TreeNode("B");
const c1 = new TreeNode("C");
const d1 = new TreeNode("D");
const e1 = new TreeNode("E");
const f1 = new TreeNode("F");
const g1 = new TreeNode("G");
a1.left = b1;
a1.right = c1;
b1.left = d1;
b1.right = e1;
c1.left = f1;
c1.right = g1;

const a2 = new TreeNode("A");
const b2 = new TreeNode("B");
const c2 = new TreeNode("C");
const d2 = new TreeNode("D");
const e2 = new TreeNode("E");
const f2 = new TreeNode("F");
const g2 = new TreeNode("Z");
a2.left = b2;
a2.right = c2;
b2.left = d2;
b2.right = e2;
c2.left = f2;
c2.right = g2;

/**
 *      a1           a2
 *      A            A
 *    /   \        /   \
 *   B     C      B     C
 *  / \   / \    / \   / \
 * D   E F   G  D   E F   Z
 */

/**
 * 比较两个二叉树是否相同
 * @param root1 第一个二叉树
 * @param root2 第二个二叉树
 * @returns {boolean|*}
 */
function compareTree(root1, root2) {
  if (root1 == root2) return true;
  if (root1 == null && root2 != null || root1 != null && root2 == null) return false;
  if (root1?.val != root2?.val) return false;
  return compareTree(root1?.left, root2?.left) && compareTree(root1?.right, root2?.right);
}

console.log(compareTree(a1, a2));
